package com.example.exp4;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.exp4.dao.PersonDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private PersonDao dao;
    private ListView listView;
    private List<Map<String,Object>> listData;
    private SimpleAdapter listAdapter;
    private EditText et_name;
    private EditText et_age;
    private String dataId;

    @Override
    public <T extends View> T findViewById(int id) {
        return super.findViewById(id);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        Button bt_add = findViewById(R.id.submit);
        bt_add.setOnClickListener(this);
        Button bt_update = findViewById(R.id.update);
        bt_update.setOnClickListener(this);
        Button bt_delete = findViewById(R.id.delete);
        bt_delete.setOnClickListener(this);


        et_name = findViewById(R.id.name);
        et_age = findViewById(R.id.age);

        dao = new PersonDao(this);
        // 添加初始数据
        if (dao.getCount() == 0) {
            dao.insert("张三",23);
            dao.insert("李四",13);
            dao.insert("王五",21);
            dao.insert("小儿牛",18);
        }
        // 显示数据
        displayRecords();

    }

    private void displayRecords() {
        listView = this.findViewById(R.id.list);
        listData = new ArrayList<>();
        Cursor cursor = dao.selectAll();
        // 遍历结果集
        while (cursor.moveToNext()) {
//            获取字段值
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));

            Map listItem = new HashMap<String,Object>();
            listItem.put("id", id);
            listItem.put("name", name);
            listItem.put("age", age);
            listData.add(listItem);//添加一条数据
        }

        listAdapter = new SimpleAdapter(this, listData
                , R.layout.list_item
                , new String[]{"id", "name", "age"}
                , new int[]{R.id.p_id, R.id.p_name, R.id.p_age}
        );
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String,Object> item = (Map<String, Object>) listAdapter.getItem(position);
                et_name.setText(item.get("name").toString());
                et_age.setText(item.get("age").toString());
                Log.i("选择了数据项：",item.get("id").toString());
                dataId = item.get("id").toString();
            }
        });

    }


    @Override
    public void onClick(View v) {
        String p_name = et_name.getText().toString().trim();
        int p_age = Integer.parseInt(et_age.getText().toString().trim());
        if (dataId != null) {
            switch (v.getId()) {
                case R.id.submit:
                    dao.insert(p_name,p_age);
                    Toast.makeText(getApplicationContext(),"添加成功！",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.update:
                    dao.update(dataId,p_name,p_age);
                    Toast.makeText(getApplicationContext(),"更新成功！",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.delete:
                    dao.delete(dataId);
                    Toast.makeText(getApplicationContext(),"删除成功！",Toast.LENGTH_SHORT).show();
                    et_name.setText(null);
                    et_age.setText(null);
                    dataId = null;
                    break;
            }
        }
        // 未选择数据项
        else {
            // 点击了添加
            if (v.getId() == R.id.submit) {
                if ("".equals(p_name) || "".equals(p_age)) {
                    Toast.makeText(getApplicationContext(),"姓名或年龄不能为空！",Toast.LENGTH_SHORT).show();
                }else {
                    dao.insert(p_name,p_age);
                }
             }else {    // 删除或修改
                Toast.makeText(getApplicationContext(),"请先选择要操作的数据项",Toast.LENGTH_SHORT).show();
            }
        }
        displayRecords();
    }
}